<?php

namespace App\Http\Controllers\Admin;

use App\Models\ResourceAudioInfo;
use App\Validate\ResourceAudioInfoValidate;
use Illuminate\Support\Facades\DB;

/**
 * 音频管理
 */
class ResourceAudioManageController extends CommonController
{
    public $model = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->validate = new ResourceAudioInfoValidate();
        $this->model = new ResourceAudioInfo();
    }

    /**
     * 获取音频资源
     * @param keywords 检索条件
     * @param type_id 类型id
     * @param is_play 是否显示  1 显示  2 隐藏
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function lists()
    {
        $limit = $this->request->input('limit', 10);
        $page = $this->request->input('page', 1);
        $res = $this->model->lists($this->request->all());
        if (empty($res['data'])) {
            return $this->returnApi(203, '暂无数据');
        }
        //增加序号
        $res['data'] = $this->addSerialNumber($res['data'], $page, $limit);
        $res['data'] = $this->disDataSameLevel($res['data'], 'con_type', ['type_name' => 'type_name']);
        $res = $this->disPageData($res);
        return $this->returnApi(200, '获取成功', true, $res);
    }

    /**
     * 详情
     * @param guid int 资源 id
     */
    public function detail()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('detail')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $res = $this->model->detail(null, $this->request->guid);
        if (empty($res)) {
            return $this->returnApi(201, "参数传递错误");
        }

        $res = $res->toArray();
        $res = $this->disDataSameLevel($res, 'con_type', ['type_name' => 'type_name']);
        return $this->returnApi(200, "获取成功", true, $res);
    }

    /**
     * 新增
     * @param title string 名称
     * @param author string 作者
     * @param type_id string 类型
     * @param img string 封面图片
    //  * @param size string 字节大小 单位字节
    //  * @param path string 资源路径
    //  * @param postfix string 文件后缀
     * @param press string 出版社
     * @param pre_time string 出版时间
   //  * @param isbn string Isbn号
     * @param intro string 简介
     */
    public function add()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('add')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $is_exists = $this->model->nameIsExists($this->request->title, 'title');

        if ($is_exists) {
            return $this->returnApi(202, "该名称已存在");
        }
        DB::beginTransaction();
        try {
            $this->model->addResource($this->request->all());
            DB::commit();
            return $this->returnApi(200, "新增成功", true);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, "新增失败");
        }
    }

    /**
     * 修改
     * @param guid int 资源 guid
     * @param title string 名称
     * @param author string 作者
     * @param type_id string 类型
     * @param img string 封面图片
    //  * @param size string 字节大小 单位字节
    //  * @param path string 资源路径
    //  * @param postfix string 文件后缀
     * @param press string 出版社
     * @param pre_time string 出版时间
    // * @param isbn string Isbn号
     * @param intro string 简介
     */
    public function change()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('change')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        $res = $this->model->detail(null, $this->request->guid);
        if (!$res) {
            return $this->returnApi(201, "参数传递错误");
        }
        $is_exists = $this->model->nameIsExists($this->request->title, 'title', $res->id);
        if ($is_exists) {
            return $this->returnApi(202, "该名称已存在");
        }
        DB::beginTransaction();
        try {
            $this->model->changeResource($this->request->all());

            DB::commit();
            return $this->returnApi(200, "修改成功", true);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, "修改失败" . $e->getMessage() . $e->getFile() . $e->getLine());
        }
    }

    /**
     * 删除
     * @param guid int 资源 id
     */
    public function del()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('del')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        DB::beginTransaction();
        try {
            $this->model->delResource($this->request->guid);

            DB::commit();
            return $this->returnApi(200, "删除成功", true);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage());
        }
    }


    /**
     * 撤销 和 发布
     * @param guids int 资源 guid 多个逗号拼接
     * @param is_play int 发布   1 发布  2 撤销
     */
    public function cancelAndRelease()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('cancel_and_release')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        DB::beginTransaction();
        try {
            $this->model->playResource($this->request->guids, $this->request->is_play);

            $is_play = $this->request->is_play == 1 ? '发布' : '撤销';

            DB::commit();
            return $this->returnApi(200, $is_play . '成功', true);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage());
        }
    }
}
